package com.lanou.zy.projecta.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.entity.Notes;
import com.lanou.zy.projecta.util.DataChange;

import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class NotesRecyclerViewAdapter extends RecyclerView.Adapter<NotesRecyclerViewAdapter.NotesViewHolder>{
    private List<Notes.DataEntity.EntryEntity> entities;


    public void addData(List<Notes.DataEntity.EntryEntity> datas) {
        this.entities = datas;
        notifyDataSetChanged();
    }

    @Override
    public NotesViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.notes_recyclerview_item, null);
        return new NotesViewHolder(v);
    }

    @Override
    public void onBindViewHolder(NotesViewHolder holder, int position) {
        if (entities != null && entities.size() > 0) {
            Notes.DataEntity.EntryEntity n = entities.get(position);

            int num = n.getLastpost() - n.getPublish_time();
            holder.notesUsersName_Tv.setText(n.getUsername());
            holder.notesContent_Tv.setText(n.getTitle());
            holder.notesViews_Tv.setText(n.getViews());
            holder.notesCommit_Tv.setText(n.getReplys());
            holder.notesTime_Tv.setText(DataChange.getTime(n.getPublish_time(), n.getLastpost()) + "小时前");
        }

    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }

    public class NotesViewHolder extends RecyclerView.ViewHolder {
        private TextView notesContent_Tv, notesUsersName_Tv, notesTime_Tv, notesViews_Tv, notesCommit_Tv;

        public NotesViewHolder(View v) {
            super(v);
            notesTime_Tv = (TextView) v.findViewById(R.id.notesTime_Tv);
            notesViews_Tv = (TextView) v.findViewById(R.id.notesViews_Tv);
            notesCommit_Tv = (TextView) v.findViewById(R.id.notesCommit_Tv);
            notesContent_Tv = (TextView) v.findViewById(R.id.notesContent_Tv);
            notesUsersName_Tv = (TextView) v.findViewById(R.id.notesUsersName_Tv);

        }
    }
}
